package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.bean.Configuration;
import com.cskaoyan.mall.bean.bo.ExpressConfigBO;
import com.cskaoyan.mall.bean.bo.MallConfigBO;
import com.cskaoyan.mall.bean.bo.OrderConfigBO;
import com.cskaoyan.mall.bean.bo.WxConfigBO;
import com.cskaoyan.mall.bean.vo.ResultVO;
import com.cskaoyan.mall.service.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/config")
public class ConfigurationController {
    @Autowired
    ConfigurationService configurationService;

    @GetMapping("mall")
    public ResultVO selectMallInfo(){
        Map<String, String> map = configurationService.getInfoByConfigClass("cskaoyanmall_mall_%");
        return ResultVO.ok(map);
    }

    @GetMapping("express")
    public ResultVO selectExpressInfo(){
        Map<String, String> map = configurationService.getInfoByConfigClass("cskaoyanmall_express_%");
        return ResultVO.ok(map);
    }

    @GetMapping("order")
    public ResultVO selectOrderInfo(){
        Map<String, String> map = configurationService.getInfoByConfigClass("cskaoyanmall_order_%");
        return ResultVO.ok(map);
    }

    @GetMapping("wx")
    public ResultVO selectWXInfo(){
        Map<String, String> map = configurationService.getInfoByConfigClass("cskaoyanmall_wx_%");
        return ResultVO.ok(map);
    }

    @PostMapping("mall")
    public ResultVO updateMallInfo(@RequestBody MallConfigBO config){
        if(! configurationService.updateMallInfo(config)){
            return ResultVO.error(502);
        }
        return ResultVO.ok();
    }

    @PostMapping("express")
    public ResultVO updateExpressInfo(@RequestBody ExpressConfigBO config){
        if(! configurationService.updateExpressInfo(config)){
            return ResultVO.error(502);
        }
        return ResultVO.ok();
    }

    @PostMapping("order")
    public ResultVO updateOrderInfo(@RequestBody OrderConfigBO config){
        if(! configurationService.updateOrderInfo(config)){
            return ResultVO.error(502);
        }
        return ResultVO.ok();
    }

    @PostMapping("wx")
    public ResultVO updateWXInfo(@RequestBody WxConfigBO config){
        if(! configurationService.updateWXInfo(config)){
            return ResultVO.error(502);
        }
        return ResultVO.ok();
    }
}
